# myapp/views.py

from django.core.mail import send_mail
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from prometheus_client import Counter

request_counter = Counter('my_app_send_mail_count', 'Total number of SendEmailView')

class SendEmailView(APIView):
    def get(self, request):
        request_counter.inc()  # 每次 GET 请求计数加一
        return Response({"message": "Hello, World!"})
    def post(self, request):
        request_counter.inc()  # 每次 GET 请求计数加一
        subject = request.data.get('subject')
        message = request.data.get('message')
        recipient_list = request.data.get('recipient_list')

        if not subject or not message or not recipient_list:
            return Response({"error": "Please provide subject, message and recipient_list."}, status=status.HTTP_400_BAD_REQUEST)

        try:
            send_mail(
                subject,
                message,
                'siuchina@163.com',
                recipient_list,
                fail_silently=False,
            )
            return Response({"success": "Email sent successfully."}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
